package com.service;

import com.domain.BlogComment;
import com.domain.Statistic;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.CommentMapper;
import com.mapper.StatisticMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    StatisticMapper statisticMapper;

    public void pushComment(BlogComment comment) {
        commentMapper.pushComment(comment);
        Statistic statistic = statisticMapper.selectStatisticWithArticleId(comment.getArticleId());
        statistic.setCommentsNum(statistic.getCommentsNum() + 1);
        statisticMapper.updateArticleCommentsWithId(statistic);
    }

    public PageInfo<BlogComment> getComments(Integer id, int page, int count) {
        PageHelper.startPage(page, count);
        List<BlogComment> commentList = commentMapper.selectCommentWithPage(id);
        PageInfo<BlogComment> commentInfo = new PageInfo<>(commentList);
        return commentInfo;
    }
}
